document.addEventListener('DOMContentLoaded', function () {
    // 获取维修数据并显示
    fetch('http://121.40.201.6/getMaintenanceData.php')
    .then(response => response.json())
    .then(data => {
        const detailsDiv = document.getElementById('maintenanceDetails');
        data.forEach(item => {
            const detailDiv = document.createElement('div');

            // 确定红绿灯颜色
            const statusClass = item.status === "已完成维护" ? "status-complete" : "status-incomplete";

            detailDiv.innerHTML = `
                <h3>
                    <span class="status-indicator ${statusClass}"></span>
                    ${item.equipment_name} - ${item.status}
                </h3>
                <p>设备位置: ${item.location}</p>
                <p>报告时间: ${item.report_time}</p>
                <p>要求完成时间: ${item.completion_time}</p>
                <p>维修人员: ${item.maintenance_person}</p>
                <hr>
            `;
            detailsDiv.appendChild(detailDiv);
        });
    })
    .catch(error => console.error('Error fetching data:', error));  // 捕获并显示任何获取数据时的错误

    // 添加、删除、更新功能的事件处理器保持不变...
    // 添加维修记录
    document.getElementById('addButton').addEventListener('click', function() {
        fetch('http://121.40.201.6/addMaintenance.php', {
            method: 'POST',
            body: JSON.stringify({
                equipment_id: 4,  // 示例数据
                location: 'xxx',
                report_time: '2024-08-16 17:15:05',
                completion_time: '2024-08-17 17:15:08',
                maintenance_person: 'xxx',
                status: '已完成修理'
            }),
            headers: {
                'Content-Type': 'application/json'
            }
        })
        .then(response => response.text())
        .then(data => {
            console.log(data);  // 输出服务器返回的消息
            alert(data);
        })
        .catch(error => console.error('Error adding data:', error));  // 捕获并显示任何添加数据时的错误
    });

    // 删除维修记录
    document.getElementById('deleteButton').addEventListener('click', function() {
        fetch('http://121.40.201.6/deleteMaintenance.php', {
            method: 'POST',
            body: JSON.stringify({
                id: 1  // 要删除的记录ID
            }),
            headers: {
                'Content-Type': 'application/json'
            }
        })
        .then(response => response.text())
        .then(data => {
            console.log(data);  // 输出服务器返回的消息
            alert(data);
        })
        .catch(error => console.error('Error deleting data:', error));  // 捕获并显示任何删除数据时的错误
    });

    // 更新维修记录
    document.getElementById('maintainButton').addEventListener('click', function() {
        fetch('http://121.40.201.6/updateMaintenance.php', {
            method: 'POST',
            body: JSON.stringify({
                id: 1,  // 要更新的记录ID
                status: '待完成修理'
            }),
            headers: {
                'Content-Type': 'application/json'
            }
        })
        .then(response => response.text())
        .then(data => {
            console.log(data);  // 输出服务器返回的消息
            alert(data);
        })
        .catch(error => console.error('Error updating data:', error));  // 捕获并显示任何更新数据时的错误
    });

    // 显示弹窗
    document.getElementById('showFormButton').addEventListener('click', function() {
        document.getElementById('popupForm').style.display = 'block';
        document.getElementById('overlay').style.display = 'block';
    });

    // 关闭弹窗
    document.getElementById('closeButton').addEventListener('click', function() {
        document.getElementById('popupForm').style.display = 'none';
        document.getElementById('overlay').style.display = 'none';
    });

    // 提交表单数据
    document.getElementById('submitButton').addEventListener('click', function() {
        const id = document.getElementById('id').value;
        const equipment_id = document.getElementById('equipment_id').value;
        const location = document.getElementById('location').value;
        const report_time = document.getElementById('report_time').value;
        const completion_time = document.getElementById('completion_time').value;
        const maintenance_person = document.getElementById('maintenance_person').value;
        const status = document.getElementById('status').value;

        const requestBody = {
            id: id,
            equipment_id: equipment_id,
            location: location,
            report_time: report_time,
            completion_time: completion_time,
            maintenance_person: maintenance_person,
            status: status
        };

        fetch('http://121.40.201.6/addMaintenance.php', {
            method: 'POST',
            body: JSON.stringify(requestBody),
            headers: {
                'Content-Type': 'application/json'
            }
        })
        .then(response => response.text())
        .then(data => {
            console.log(data);  // 输出服务器返回的消息
            alert(data);
            document.getElementById('popupForm').style.display = 'none';
            document.getElementById('overlay').style.display = 'none';
        })
        .catch(error => console.error('Error adding data:', error));
    });
});
